package com.ggk911.pharmacy_system.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ggk911.pharmacy_system.entity.DTO.InventoryDTO;
import com.ggk911.pharmacy_system.entity.Inventory;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author ggk-911
 * @version 1.0 2023/4/11 17:10
 */
@Repository
public interface InventoryMapper extends BaseMapper<Inventory> {

    /**
     * selectPage加强
     *
     * @param page
     * @param queryWrapper
     * @param <P>
     * @return
     */
    @Select(" SELECT *," +
            " drug_info_id  as 'drugInfo.drugInfoId'," +
            " drug_info.drug_info_name  as 'drugInfo.drugInfoName'," +
            " drug_info_alias  as 'drugInfo.drugInfoAlias'," +
            " drug_info_strength  as 'drugInfo.drugInfoStrength'," +
            " drug_info_indications  as 'drugInfo.drugInfoIndications'," +
            " drug_info_adverse_reactions  as 'drugInfo.drugInfoAdverseReactions'," +
            " drug_info_interactions  as 'drugInfo.drugInfoInteractions'," +
            " drug_info_dosage  as 'drugInfo.drugInfoDosage'," +
            " drug_info_specifications  as 'drugInfo.drugInfoSpecifications'," +
            " drug_info_storage_method  as 'drugInfo.drugInfoStorageMethod'," +
            " drug_info_valid_time  as 'drugInfo.drugInfoValidTime'," +
            " drug_info_manufacturer  as 'drugInfo.drugInfoManufacturer'," +
            " drug_info_national_code  as 'drugInfo.drugInfoNationalCode'," +
            " drug_info_approval_no  as 'drugInfo.drugInfoApprovalNo'," +
            " drug_info_listing_license_holder  as 'drugInfo.drugInfoListingLicenseHolder'," +
            " drug_info_type_id as 'drugInfoTypeId'," +
            " drug_info_type_id as 'drugInfo.drugInfoType.drugInfoTypeId'," +
            " drug_info_type_name as 'drugInfo.drugInfoType.drugInfoTypeName'," +
            " drug_price as 'drugPrice.drugPrice'," +
            " drug_price.drug_info_id as 'drugPrice.drugInfoId' " +
            " FROM inventory " +
            " LEFT JOIN drug_info USING (drug_info_id) " +
            " LEFT JOIN drug_info_type USING (drug_info_type_id) " +
            " LEFT JOIN drug_price USING (drug_info_id) ${ew.customSqlSegment}")
    <P extends IPage<Inventory>> P selectPageAd(P page, @Param("ew") Wrapper<Inventory> queryWrapper);


    /**
     * 查询的selectCountDTO
     *
     * @param queryWrapper
     * @return
     */
    @Override
    Long selectCount(@Param("ew") Wrapper<Inventory> queryWrapper);

    /**
     * 查询单个
     *
     * @return
     */
    Inventory selectOneByDrugInfoId(Integer id);


    boolean updateByNoRepeat(@Param("inventoryNum") Integer inventoryNum, @Param("drugInfoId") Integer drugInfoId);

    boolean insertAd(@Param("inventoryNum") Integer inventoryNum, @Param("drugInfoId") Integer drugInfoId);

    /**
     * selectListAd
     *
     * @param queryWrapper
     * @return
     */
    List<Inventory> selectListAd(@Param("ew") Wrapper<Inventory> queryWrapper);

    /**
     * selectSumByType
     *
     * @return
     */
    List<InventoryDTO> selectSumByType();

    /**
     * 获取库存预警
     */
    int selectInventoryLimit();

    /**
     * 修改limit
     *
     * @return
     */
    boolean updateInventoryLimit(Integer limit);


}
